package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.User;
import com.symone.webcms.model.base.ResultMessage;
import com.symone.webcms.model.vo.UserInfoVO;
import com.symone.webcms.service.UserService;
import com.symone.webcms.util.CommonUtil;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/user")
@Api(value = "UserController",description = "系统用户管理")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * @description : 获取分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2018-11-18
     */
    @RequiresPermissions("sys:user:index")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public ResultMessage page(String userName, String email, String mobile, String roleId, Integer state, Integer pageSize, Integer pageNum) {
        if(pageNum == null){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }

        Page<UserInfoVO> page = new Page<UserInfoVO>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        page = userService.getUserListByParameters(page,userName,mobile,email,roleId,state);

        return ResultMessage.success("获取用户分页列表",page.getRecords(),page.getTotal());
    }

    @RequiresPermissions("sys:user:list")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResultMessage list(){
        List<User> users = userService.selectList(new EntityWrapper<User>().eq("deleted",0));
        return ResultMessage.success("用户信息列表",users);
    }

    @RequiresPermissions("sys:user:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultMessage add(User user, BindingResult br){
        if(br.hasErrors()){
            return ResultMessage.fail("添加用户信息失败! " + br.getFieldError().getDefaultMessage());
        } else {
            int checkCount = userService.selectCount(new EntityWrapper<User>()
                    .ne("id" , user.getId())
                    .andNew().eq("user_name",user.getUserName())
                    .or().eq("email",user.getEmail())
                    .or().eq("mobile",user.getMobile())
            );
            if(checkCount > 0 ){
                return ResultMessage.fail("添加用户信息失败:用户名、Email或手机号码重复！");
            }

            user.setPassword(CommonUtil.encodePassword(user.getPassword()));
            userService.insert(user);
            return ResultMessage.success("添加用户信息成功!");
        }
    }

    /**
     * http://localhost:8080/user/edit
     * {id=2,username="admin",nickname="管理员admin"}
     * @param user
     * @return
     */
    @RequiresPermissions("sys:user:edit")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResultMessage update(User user, BindingResult br) {
        if(br.hasErrors()){
            return ResultMessage.fail("用户信息更新失败!"+ br.getFieldError().getDefaultMessage());
        } else {
            int checkCount = userService.selectCount(new EntityWrapper<User>()
                    .ne("id" , user.getId())
                    .andNew().eq("user_name",user.getUserName())
                    .or().eq("email",user.getEmail())
                    .or().eq("mobile",user.getMobile())

            );
            if(checkCount > 0 ){
                return ResultMessage.fail("用户信息更新失败:用户名、Email或手机号码重复！");
            }else {
                userService.updateUser(user);
                return ResultMessage.success("用户信息更新成功!");
            }
        }
    }

    @RequiresPermissions("sys:user:info")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public ResultMessage info(long id){
        return ResultMessage.success("获取用户信息成功!",userService.selectById(id));
    }

    @RequiresPermissions("sys:user:remove")
    @RequestMapping(value = "/removeFlag", method = RequestMethod.PUT)
    public ResultMessage removeFlag(long id){
        if(userService.deleteById(id)) {
            return ResultMessage.success("逻辑用户信息删除成功！");
        } else {
            return ResultMessage.fail("逻辑用户信息删除失败！");
        }
    }

    @RequiresPermissions("sys:user:remove")
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    public ResultMessage remove(long id){
        if(userService.deleteById(id)) {
            return ResultMessage.success("物理用户信息删除成功！");
        } else {
            return ResultMessage.fail("物理用户信息删除失败！");
        }
    }

    @RequestMapping(value = "/changePassword", method = RequestMethod.POST)
    public ResultMessage changePassword(Long id,String userName,String oldPassword,String password){
        if( CommonUtil.checkPassword( oldPassword, getSessionUser().getPassword()) ){
            if (userService.changePassword(id, userName, password)) {
                return ResultMessage.success("修改用户密码成功！");
            } else {
                return ResultMessage.fail("修改用户密码失败！");
            }
        }else {
            return ResultMessage.fail("原密码检验失败！");
        }
    }
}